package com.achievement.project.system.choose.controller;

import java.util.List;

import com.achievement.project.monitor.server.domain.Sys;
import com.achievement.project.system.student.domain.Student;
import com.achievement.project.system.student.service.IStudentService;
import com.achievement.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.achievement.framework.aspectj.lang.annotation.Log;
import com.achievement.framework.aspectj.lang.enums.BusinessType;
import com.achievement.project.system.choose.domain.CourseChoose;
import com.achievement.project.system.choose.service.ICourseChooseService;
import com.achievement.framework.web.controller.BaseController;
import com.achievement.framework.web.domain.AjaxResult;
import com.achievement.common.utils.poi.ExcelUtil;
import com.achievement.framework.web.page.TableDataInfo;

/**
 * 课程管理Controller
 *
 * @author zkai
 * @date 2021-04-27
 */
@Controller
@RequestMapping("/system/choose")
public class CourseChooseController extends BaseController
{
    private String prefix = "system/choose";

    @Autowired
    private ICourseChooseService courseChooseService;

    @Autowired
    private IStudentService studentService;

    @RequiresPermissions("system:choose:view")
    @GetMapping()
    public String choose()
    {
        return prefix + "/choose";
    }

    /**
     * 查询课程管理列表
     */
//    @RequiresPermissions("system:choose:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CourseChoose courseChoose)
    {
        startPage();
        List<CourseChoose> list = courseChooseService.selectCourseChooseList(courseChoose);
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
    @RequiresPermissions("system:choose:export")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CourseChoose courseChoose)
    {
        List<CourseChoose> list = courseChooseService.selectCourseChooseList(courseChoose);
        ExcelUtil<CourseChoose> util = new ExcelUtil<CourseChoose>(CourseChoose.class);
        return util.exportExcel(list, "课程管理数据");
    }

    /**
     * 新增课程管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存课程管理
     */
    @RequiresPermissions("system:choose:add")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CourseChoose courseChoose)
    {
        return toAjax(courseChooseService.insertCourseChoose(courseChoose));
    }

    /**
     * 修改课程管理
     */
    @GetMapping("/edit/{courseId}")
    public String edit(@PathVariable("courseId") Integer courseId, ModelMap mmap)
    {
        CourseChoose courseChoose = courseChooseService.selectCourseChooseById(courseId);
        mmap.put("courseChoose", courseChoose);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程管理
     */
    @RequiresPermissions("system:choose:edit")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CourseChoose courseChoose)
    {
        return toAjax(courseChooseService.updateCourseChoose(courseChoose));
    }

    /**
     * 删除课程管理
     */
    @RequiresPermissions("system:choose:remove")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(courseChooseService.deleteCourseChooseByIds(ids));
    }


    /**
     * 跳转分配学生列表页面
     */
    @RequiresPermissions("system:choose:assignStudents")
    @GetMapping("/assignStudents/{courseId}")
    public String assignStudents(@PathVariable("courseId") Integer courseId, ModelMap mmap)
    {
        CourseChoose courseChoose = courseChooseService.selectCourseChooseById(courseId);
        mmap.put("courseChoose", courseChoose);
        return prefix + "/assignStudents";
    }


    /**
     * 分配学生列表
     */
    @RequiresPermissions("system:choose:assignStudents:list")
    @PostMapping("/assignStudents/list")
    @ResponseBody
    public TableDataInfo assignStudentsList(@RequestParam Integer courseId,String no,String name)
    {

        startPage();
        List<Student> list = studentService.selectStudentListByCid(courseId,no,name);
        return getDataTable(list);
    }


    /**
     * 跳转分配学生列表页面
     */
    @RequiresPermissions("system:choose:assignStudents:unallocatedList")
    @GetMapping("/assignStudents/selectStudents/{courseId}")
    public String selectStudents(@PathVariable("courseId") Integer courseId, ModelMap mmap)
    {
        CourseChoose courseChoose = courseChooseService.selectCourseChooseById(courseId);
        mmap.put("courseChoose", courseChoose);
        return prefix + "/selectStudent";
    }


    /**
     * 查询未分配学生列表
     */
    @RequiresPermissions("system:choose:assignStudents:unallocatedList:list")
    @PostMapping("/assignStudents/unallocatedList")
    @ResponseBody
    public TableDataInfo unallocatedList(Student student)
    {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 批量选择学生
     */
    @Log(title = "批量选择学生", businessType = BusinessType.GRANT)
    @PostMapping("/assignStudents/unallocatedList/selectStudentsAll")
    @ResponseBody
    public AjaxResult selectStudentsAll(Integer courseId, String sIds)
    {

        return toAjax(courseChooseService.insertCourseBySid(courseId,sIds));
    }

    /**
     * 删除课程管理
     */
    @RequiresPermissions("system:choose:assignStudents:remove")
    @Log(title = "课程--分配学生--删除", businessType = BusinessType.DELETE)
    @PostMapping( "/assignStudents/remove")
    @ResponseBody
    public AjaxResult removeOne(Integer courseId,Integer sId)
    {
        return toAjax(courseChooseService.deleteCourseStudentOne(courseId,sId));
    }


    /**
     * 删除课程管理
     */
    @RequiresPermissions("system:choose:assignStudents:removeAll")
    @Log(title = "课程--分配学生--删除选中", businessType = BusinessType.DELETE)
    @PostMapping( "/assignStudents/removeAll")
    @ResponseBody
    public AjaxResult removeAll(Integer courseId,String sIds)
    {
        return toAjax(courseChooseService.deleteCourseStudentAll(courseId,sIds));
    }

}
